package com.hkyl.hospital.web.controller;

import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Role;
import com.hkyl.hospital.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author whd
 * @date 2023/07/08 18:10:52
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("save")
    public ResultVo save(@RequestBody Role role) {
        int rows = roleService.save(role);
        return rows > 0 ? ResultVo.success("添加成功", role) : ResultVo.failure("添加失败");
    }

    @PostMapping("findAll")
    public ResultVo findAll() {
        return ResultVo.success(roleService.findAll());
    }

    @PostMapping("delete")
    public ResultVo delete(@RequestBody String roleId) {
        if (!StringUtils.hasText(roleId)) {
            throw new RuntimeException("不存在输入的id");
        }
        int rows = roleService.delete(Integer.valueOf(roleId));
        return rows > 0 ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }

    @PostMapping("update")
    public ResultVo update(@RequestBody Role role) {
        int rows = roleService.update(role);
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }

}
